<?php

namespace app\merchant\controller\goods;

use app\merchant\controller\Base;
use app\merchant\model\Comment as CommentModel;

/**
 * 商品评价管理
 * Class Comment
 * @package app\store\controller\goods
 */
class Comment extends Base
{
    /**
     * 评价列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new CommentModel;
        $list = $model->getMerchantList();

        return $this->renderSuccess(compact('list'));
    }


    /**
     * 评价详情
     * @param $comment_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();
        if(!isset($param['comment_id']) || isZeroNumeric($param['comment_id'])){
            return $this->renderError([],"参数错误");
        }
        $model = new CommentModel;

        // 评价详情
        $model = $model->merchantDetail($param['comment_id']);
        if(!$model) return $this->renderError([],"评价不存在，请刷新重试");
        return $this->renderSuccess($model);

    }

    /**
     * 删除评价
     * @param $comment_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        if(!isset($param['comment_id']) || isZeroNumeric($param['comment_id'])){
            return $this->renderError([],"参数错误");
        }

        // 评价详情
        $model = CommentModel::merchantDetail($param['comment_id']);
        if(!$model) return $this->renderError([],"评价不存在，请刷新重试");

        if (!$model->setDelete()) {
            return $this->renderError([],'删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }


    /**
     * 评价状态修改
     * @param $comment_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function state()
    {
        $param = $this->postData();
        if(!isset($param['comment_id']) || isZeroNumeric($param['comment_id'])){
            return $this->renderError([],"参数错误");
        }
        if(!isset($param['status']) || isZeroNumeric($param['status'])){
            return $this->renderError([],"参数错误");
        }

        // 评价详情
        $model = CommentModel::merchantDetail($param['comment_id']);
        $model->setState($param['status']);

        return $this->renderSuccess([],'操作成功');
    }

}